package com.jz.community.moduleshoppingguide.home.ui.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.commview.view.RelativePopupWindow;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.home.bean.CategoriesBean;
import com.jz.community.moduleshoppingguide.home.ui.adapter.HomeTabAdapter;
import com.jz.community.moduleshoppingguide.nearshop.utils.FlowLayoutManager;
import com.jz.community.moduleshoppingguide.nearshop.utils.SpaceItemDecoration;

import java.util.List;


public class HomeTabPopupWindow extends RelativePopupWindow {

    private Context context;
    private List<CategoriesBean> type_list;
    ICategoriesClick iCategoriesClick;
    HomeTabAdapter adapter_type;

    public HomeTabPopupWindow(Context context, List<CategoriesBean> type_list) {
        super(context);
        this.type_list = type_list;
        this.context = context;
        // 设置SelectPicPopupWindow的View
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        View view = View.inflate(context, R.layout.pop_home_tab, null);
        setContentView(view);
        initView(view);
    }

    private void initView(View view) {
        RecyclerView popRv = view.findViewById(R.id.pop_home_rv);
        ImageView pop_close = view.findViewById(R.id.pop_home_title_iv);
        FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
        popRv.setHasFixedSize(true);
        popRv.setFocusable(false);
        popRv.setLayoutManager(flowLayoutManager);
        popRv.addItemDecoration(new SpaceItemDecoration(SHelper.dp2px(context, 5)));
        adapter_type = new HomeTabAdapter(R.layout.module_shoppingguide_item_pop_home, type_list);
        popRv.setAdapter(adapter_type);
        adapter_type.setOnItemClickListener((adapter, view1, position) -> {
            adapter_type.setChick(position);
            iCategoriesClick.onItemClick(type_list.get(position), position);
            dismiss();
        });
        pop_close.setOnClickListener(v -> dismiss());
        View pop_shade = view.findViewById(R.id.pop_near_shop_shade);
        pop_shade.setOnClickListener(v -> dismiss());
    }


    public void setAdapterChoose(int position){
        adapter_type.setChick(position);
    }


    public interface ICategoriesClick {
        void onItemClick(CategoriesBean categoriesBean, int position);
    }

    public void setiCategoriesClick(ICategoriesClick iCategoriesClick) {
        this.iCategoriesClick = iCategoriesClick;
    }
}
